package com.dlsp.hotel.controller.admin.system;

import com.dlsp.hotel.bean.admin.User;
import com.dlsp.hotel.service.admin.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;

import javax.servlet.http.HttpServletRequest;

/**
 * @author:fanxh
 * @date: 2022/1/1/23:08
 * 后台处理用户Controller
 */
@Controller
@SessionAttributes(names = "user")
@RequestMapping("/system/admin")
public class SystemController {
    @Autowired
    private UserService userService;
    /**
     * 后台登录页面
     * @return
     */
    @RequestMapping("/loginUi")
    public String loginUi(){
        return "/pages/system/login";
    }

    @RequestMapping("/indexUi")
    public String indexUi(){
        return "pages/system/index";
    }
    @RequestMapping("/welcomeUi")
    public String welcomeUi(){
        return "pages/system/welcome";
    }

    @RequestMapping("/editPwdUi")
    public String editPwdUi(){
        return "pages/system/edit_password";
    }

    /**
     * 登录方法
     */
    @RequestMapping("/login")
    public String login(String username, String password, String cpacha, Model model, HttpServletRequest request){
        User user = userService.login(username, password);
        Object loginCpacha = request.getSession().getAttribute("loginCpacha");

        if (user!=null){
            if(!cpacha.toUpperCase().equals(loginCpacha.toString().toUpperCase())){
                return "redirect:/system/admin/loginUi";
            }
            model.addAttribute("user", user);
            return "redirect:/system/admin/indexUi";
        }
        return "redirect:/system/admin/loginUi";
    }

    /**
     * 退出登录
     * @param sessionStatus
     * @return
     */
    @RequestMapping("/logout")
    public String logout(SessionStatus sessionStatus) {
        sessionStatus.setComplete();
        return "redirect:/system/admin/loginUi";
    }
}
